home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / misc / sci / eliza.lha / eliza.c < prev    next >
C/C++ Source or Header  |  1993-06-07  |  20KB  |  690 lines

  1. /**************************************************************************
  2.  * Eliza - The classic artificial intelligence therapist                  *
  3.  * Creative Computing                                                     *
  4.  * Morristown, New Jersey                                                 *
  5.  * Adapted for IBM PC (in BASIC) by Patricia Danielson and Paul Hashfield *
  6.  * Ported to C for maximum compatibility by James Williams                *
  7.  * Copyright (C) 1995  James Williams                                     *
  8.  *                                                                        *
  9.  * This program is free software; you can redistribute it and/or modify   *
  10.  * it under the terms of the GNU General Public License as published by   *
  11.  * the Free Software Foundation; either version 2 of the License, or      *
  12.  * (at your option) any later version.                                    *
  13.  *                                                                        *
  14.  * This program is distributed in the hope that it will be useful,        *
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
  17.  * GNU General Public License for more details.                           *
  18.  *                                                                        *
  19.  * You should have received a copy of the GNU General Public License      *
  20.  * along with this program; if not, write to the Free Software            *
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              *
  22.  **************************************************************************/
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29.  
  30. #define isws(x)  ((x) == ' ' || (x) == '\t' || (x) == '\n')
  31.  
  32. void init(void);
  33. void exchange_words(char *string, int word_start, int word_len,
  34.                     char *new_word);
  35. void ucase(char *string);
  36. void reformat_input(void);
  37. int get_input(void);
  38. char *find_keyword(void);
  39. char *conjugate(char *found);
  40. void response(char *tail);
  41.  
  42.  
  43. #define MAX_BUFFER 255
  44. char input_string_buffer[MAX_BUFFER+4] = "  ";
  45. char *input_string = input_string_buffer+2;
  46. char last_input_string[MAX_BUFFER];
  47.  
  48. char *keyword[] =
  49. {
  50.     "THANK", "CAN YOU ", "CAN I ", "YOU ARE ", "YOU'RE ", "I DON'T ",
  51.     "I FEEL ", "WHY DON'T YOU ", "WHY CAN'T I ", "ARE YOU ",
  52.     "I CAN'T ", "I AM ", "I'M ", "YOUR ", "I WANT ", "WHAT ",
  53.     "HOW ", "WHO ", "WHERE ", "WHEN ", "WHY ", "NAME", "CAUSE ",
  54.     "SORRY ", "DREAM", "HELLO ", "HI ", "MAYBE ", "NO ", "NOT ",
  55.     "NOTHING ", "YOU ", "ALWAYS ", "THINK ", "ALIKE ", "YES ",
  56.     "FRIEND ", "COMPUTER", "NOKEYFOUND"
  57. };
  58.  
  59. char *wordin[] =
  60. {
  61.     " ARE ", " WERE ", " YOUR ", " I'VE ", " I'M ", " ME ",
  62.     " AM ", " WAS ", " I ", " MY ", " YOU'VE ", " YOU'RE ", " YOU "
  63. };
  64. char *wordout[] =
  65. {
  66.     " AM ", " WAS ", " MY ", " YOU'VE ", " YOU'RE ", " YOU ",
  67.     " ARE ", " WERE ", " YOU ", " YOUR ", " I'VE ", " I'M ", " ME "
  68. };
  69.  
  70. char *replies[] =
  71. {
  72.     /* THANK */
  73.     "YOU'RE WELCOME",
  74.     "I'M GLAD I COULD HELP",
  75.  
  76.     /* CAN YOU */
  77.     "DON'T YOU BELIEVE THAT I CAN*",
  78.     "PERHAPS YOU BELIEVE I SHOULD*",
  79.     "YOU WANT ME TO BE ABLE TO*",
  80.  
  81.     /* CAN I */
  82.     "PERHAPS YOU DON'T WANT TO*",
  83.     "DO YOU WANT TO BE ABLE TO*",
  84.  
  85.     /* YOU ARE, YOU'RE */
  86.     "WHAT MAKES YOU THINK I AM*",
  87.     "DOES IT PLEASE YOU TO BELIEVE I AM*",
  88.     "PERHAPS YOU WOULD LIKE TO BE*",
  89.     "DO YOU SOMETIMES WISH YOU WERE*",
  90.  
  91.     /* I DON'T */
  92.     "DON'T YOU REALLY*",
  93.     "WHY DON'T YOU*",
  94.     "DO YOU WISH TO BE ABLE TO*",
  95.     "DOES IT TROUBLE YOU TO NOT*",
  96.  
  97.     /* I FEEL */
  98.     "DO YOU OFTEN FEEL*",
  99.     "WHY DO YOU THINK YOU FEEL*",
  100.     "DO YOU ENJOY FEELING*",
  101.  
  102.     /* WHY DON'T YOU */
  103.     "DO YOU REALLY BELIEVE I DON'T*",
  104.     "PERHAPS IN GOOD TIME I WILL*",
  105.     "DO YOU WANT ME TO*",
  106.  
  107.     /* WHY CAN'T I */
  108.     "DO YOU THINK YOU SHOULD BE ABLE TO*",
  109.     "WHY CAN'T YOU*",
  110.  
  111.     /* ARE YOU */
  112.     "WHY ARE YOU INTERESTED IN WHETHER OR NOT I AM*",
  113.     "WOULD YOU PREFER IF I WERE NOT*",
  114.     "PERHAPS IN YOUR FANTASIES I AM*",
  115.  
  116.     /* I CAN'T */
  117.     "HOW DO YOU KNOW YOU CAN'T*",
  118.     "HAVE YOU TRIED?",
  119.     "PERHAPS YOU CAN NOW*",
  120.  
  121.     /* I AM, I'M */
  122.     "DID YOU COME TO ME BECAUSE YOU ARE*",
  123.     "HOW LONG HAVE YOU BEEN*",
  124.     "DO YOU BELIEVE IT IS NORMAL TO BE*",
  125.     "DO YOU ENJOY BEING*",
  126.  
  127.     /* YOUR */
  128.     "WHY ARE YOU CONCERNED ABOUT MY*",
  129.     "WHAT ABOUT YOUR OWN*",
  130.  
  131.     /* I WANT */
  132.     "WHAT WOULD IT MEAN TO YOU IF YOU GOT*",
  133.     "WHY DO YOU WANT*",
  134.     "SUPPOSE YOU SOON GOT*",
  135.     "WHAT IF YOU NEVER GOT*",
  136.     "I SOMETIMES ALSO WANT*",
  137.  
  138.     /* HOW, WHO, WHERE, WHEN, WHY */
  139.     "WHY DO YOU ASK?",
  140.     "DOES THAT QUESTION INTEREST YOU?",
  141.     "WHAT ANSWER WOULD PLEASE YOU THE MOST?",
  142.     "WHAT DO YOU THINK?",
  143.     "ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
  144.     "WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
  145.     "HAVE YOU ASKED ANYONE ELSE?",
  146.     "HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
  147.     "WHAT else COMES TO MIND WHEN YOU ASK THAT?",
  148.  
  149.     /* NAME */
  150.     "NAMES DON'T INTEREST ME.",
  151.     "I DON'T CARE ABOUT NAMES --PLEASE GO ON.",
  152.  
  153.     /* CAUSE */
  154.     "IS THAT THE REAL REASON?",
  155.     "DON'T ANY OTHER REASONS COME TO MIND?",
  156.     "DOES THAT REASON EXPLAIN ANYTHING ELSE?",
  157.     "WHAT OTHER REASONS MIGHT THERE BE?",
  158.  
  159.     /* SORRY */
  160.     "PLEASE DON'T APOLOGIZE!",
  161.     "APOLOGIES ARE NOT NECESSARY.",
  162.     "WHAT FEELINGS DO YOU HAVE WHEN YOU APOLOGIZE?",
  163.     "DON'T BE SO DEFENSIVE!",
  164.  
  165.     /* DREAM */
  166.     "WHAT DOES THAT DREAM SUGGEST TO YOU?",
  167.     "DO YOU DREAM OFTEN?",
  168.     "WHAT PERSONS APPEAR IN YOUR DREAMS?",
  169.     "ARE YOU DISTURBED BY YOUR DREAMS?",
  170.  
  171.     /* HELLO, HI */
  172.     "HOW DO YOU DO ...PLEASE STATE YOUR PROBLEM.",
  173.  
  174.     /* MAYBE */
  175.     "YOU DON'T SEEM QUITE CERTAIN.",
  176.     "WHY THE UNCERTAIN TONE?",
  177.     "CAN'T YOU BE MORE POSITIVE?",
  178.     "YOU AREN'T SURE?",
  179.     "DON'T YOU KNOW?",
  180.  
  181.     /* NO, NOT, NOTHING */
  182.     "ARE YOU SAYING NO JUST TO BE NEGATIVE?",
  183.     "YOU ARE BEING A BIT NEGATIVE.",
  184.     "WHY NOT?",
  185.     "ARE YOU SURE?",
  186.     "WHY NO?",
  187.  
  188.     /* YOU */
  189.     "WE WERE DISCUSSING YOU--NOT ME.",
  190.     "OH, I*",
  191.     "YOU'RE NOT REALLY TALKING ABOUT ME, ARE YOU?",
  192.  
  193.     /* ALWAYS */
  194.     "CAN YOU THINK OF A SPECIFIC EXAMPLE?",
  195.     "WHEN?",
  196.     "WHAT ARE YOU THINKING OF?",
  197.     "REALLY, ALWAYS?",
  198.  
  199.     /* THINK */
  200.     "DO YOU REALLY THINK SO?",
  201.     "BUT YOU ARE NOT SURE*",
  202.     "DO YOU DOUBT*",
  203.  
  204.     /* ALIKE */
  205.     "IN WHAT WAY?",
  206.     "WHAT RESEMBLANCE DO YOU SEE?",
  207.     "WHAT DOES THE SIMILARITY SUGGEST TO YOU?",
  208.     "WHAT OTHER CONNECTIONS DO YOU SEE?",
  209.     "COULD THERE REALLY BE SOME CONNECTION?",
  210.     "HOW?",
  211.  
  212.     /* YES */
  213.     "ARE YOU SURE?",
  214.     "I SEE.",
  215.     "I UNDERSTAND.",
  216.     "YOU SEEM QUITE POSITIVE.",
  217.  
  218.     /* FRIEND */
  219.     "WHY DO YOU BRING UP THE TOPIC OF FRIENDS?",
  220.     "DO YOUR FRIENDS WORRY YOU?",
  221.     "DO YOUR FRIENDS PICK ON YOU?",
  222.     "ARE YOU SURE YOU HAVE ANY FRIENDS?",
  223.     "DO YOU IMPOSE ON YOUR FRIENDS?",
  224.     "PERHAPS YOUR LOVE FOR FRIENDS WORRIES YOU.",
  225.  
  226.     /* COMPUTER */
  227.     "DO COMPUTERS WORRY YOU?",
  228.     "ARE YOU TALKING ABOUT ME IN PARTICULAR?",
  229.     "ARE YOU FRIGHTENED BY MACHINES?",
  230.     "WHY DO YOU MENTION COMPUTERS?",
  231.     "WHAT DO YOU THINK MACHINES HAVE TO DO WITH YOUR PROBLEM?",
  232.     "DON'T YOU THINK COMPUTERS CAN HELP PEOPLE?",
  233.     "WHAT IS IT ABOUT MACHINES THAT WORRIES YOU?",
  234.  
  235.     /* NOKEYFOUND */
  236.     "WHAT DOES THAT SUGGEST TO YOU?",
  237.     "I SEE.",
  238.     "I'M NOT SURE I UNDERSTAND YOU FULLY.",
  239.     "COME COME ELUCIDATE YOUR THOUGHTS.",
  240.     "CAN YOU ELABORATE ON THAT?",
  241.     "THAT IS QUITE INTERESTING.",
  242.     "SAY, DO YOU HAVE ANY PSYCHOLOGICAL PROBLEMS?"
  243. };
  244.  
  245.  
  246. int num_of_responses[] = {
  247. /* This tells how many responses are available for each keyword.  A -1
  248.  * means the word is synonymous with the previous word (don't use -1 for
  249.  * the first entry!!!)
  250.  */
  251.  
  252.     2,                /* THANK */
  253.     3,                /* CAN YOU */
  254.     2,                /* CAN I */
  255.     4,-1,             /* YOU ARE, YOU'RE */
  256.     4,                /* I DON'T */
  257.     3,                /* I FEEL */
  258.     3,                /* WHY DON'T YOU */
  259.     2,                /* WHY CAN'T I */
  260.     3,                /* ARE YOU */
  261.     3,                /* I CAN'T */
  262.     4,-1,             /* I AM, I'M */
  263.     2,                /* YOUR */
  264.     5,                /* I WANT */
  265.     9,-1,-1,-1,-1,-1, /* WHAT, HOW, WHO, WHERE, WHEN, WHY */
  266.     2,                /* NAME */
  267.     4,                /* CAUSE */
  268.     4,                /* SORRY */
  269.     4,                /* DREAM */
  270.     1,-1,             /* HELLO, HI */
  271.     5,                /* MAYBE */
  272.     5,-1,-1,          /* NO, NOT, NOTHING */
  273.     3,                /* YOU */
  274.     4,                /* ALWAYS */
  275.     3,                /* THINK */
  276.     6,                /* ALIKE */
  277.     4,                /* YES */
  278.     6,                /* FRIEND */
  279.     7,                /* COMPUTER */
  280.     7                 /* NOKEYFOUND */
  281. };
  282.  
  283. int *first_response;
  284. int *current_response;
  285. int *last_response;
  286.  
  287.  
  288. int n1,  /* Number of elements in keywords (minus 1) */
  289.     n2,  /* Number of elements in wordin and wordout (minus 1) */
  290.     key;
  291.  
  292. int keyword_size, num_of_responses_size, wordin_size, wordout_size;
  293.  
  294. /**************************************************************
  295.  function init
  296.  Initializes some critical variables
  297.  **************************************************************/
  298. void init(void)
  299. {
  300.     int loop, response_size, response_number = 0, number = 0;
  301.     int num_of_responses_size;
  302.  
  303.     /* Set n1 and n2 and some sizes */
  304.     num_of_responses_size = sizeof(num_of_responses) / sizeof(int);
  305.     keyword_size = sizeof(keyword) / sizeof(int);
  306.     wordin_size = sizeof(wordin) / sizeof(char *);
  307.     wordout_size = sizeof(wordout) / sizeof(char *);
  308.     n1 = keyword_size - 1;
  309.     n2 = wordin_size - 1;
  310.     if (keyword_size != num_of_responses_size)
  311.     {
  312.         printf("The sizes of the arrays \"keyword\" and \"num_of_responses\"\n");
  313.         printf("do not match!\n\n");
  314.         printf("sizeof(keyword) = %li, sizeof(num_of_responses) = %li\n",
  315.                 keyword_size, num_of_responses_size);
  316.         exit (11);
  317.     }
  318.  
  319.     if (wordin_size != wordout_size)
  320.     {
  321.         printf("The sizes of the arrays \"wordin\" and \"wordout\"\n");
  322.         printf("do not match!\n\n");
  323.         printf("sizeof(wordin) = %li, sizeof(wordout) = %li\n",
  324.                 wordin_size, wordout_size);
  325.         exit (11);
  326.     }
  327.  
  328.     /* Allocate the memory for first_response, current_response, and last_response */
  329.     response_size = sizeof(num_of_responses);
  330.     if ((first_response = (int *)malloc(response_size)) == NULL)
  331.     {
  332.         printf("Unable to allocate sufficient memory\n");
  333.         exit (12);
  334.     }
  335.  
  336.     if ((current_response = (int *)malloc(response_size)) == NULL)
  337.     {
  338.         printf("Unable to allocate sufficient memory\n");
  339.         exit (12);
  340.     }
  341.  
  342.     if ((last_response = (int *)malloc(response_size)) == NULL)
  343.     {
  344.         printf("Unable to allocate sufficient memory\n");
  345.         exit (12);
  346.     }
  347.  
  348.     /* Set first_response, current_response, and last_response */
  349.     for (loop = 0; loop < num_of_responses_size; loop++)
  350.     {
  351.         if (num_of_responses[loop] == -1)
  352.         {
  353.             response_number -= number;  /* Synonym of last word */
  354.         }
  355.         else
  356.         {
  357.             number = num_of_responses[loop];
  358.         }
  359.  
  360.         first_response[loop] = response_number;
  361.         current_response[loop] = response_number;
  362.         last_response[loop] = response_number + number - 1;
  363.         response_number += number;
  364.     }
  365.  
  366.     /* Print the GNU license */
  367.     puts("Eliza - The classic artificial intelligence therapist");
  368.     puts("Creative Computing");
  369.     puts("Morristown, New Jersey");
  370.     puts("Adapted for IBM PC (in BASIC) by Patricia Danielson and Paul Hashfield");
  371.     puts("Ported to C for maximum compatibility by James Williams");
  372.     puts("Copyright (C) 1995  James Williams");
  373.     puts("");
  374.     puts("This program is free software; you can redistribute it and/or modify");
  375.     puts("it under the terms of the GNU General Public License as published by");
  376.     puts("the Free Software Foundation; either version 2 of the License, or");
  377.     puts("(at your option) any later version.");
  378.     puts("");
  379.     puts("This program is distributed in the hope that it will be useful,");
  380.     puts("but WITHOUT ANY WARRANTY; without even the implied warranty of");
  381.     puts("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the");
  382.     puts("GNU General Public License for more details.");
  383.     puts("");
  384.     puts("You should have received a copy of the GNU General Public License");
  385.     puts("along with this program; if not, write to the Free Software");
  386.     puts("Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.");
  387.     puts("");
  388.  
  389.     /* That should be it */
  390. }
  391.  
  392.  
  393. /**************************************************************
  394.  function exchange_words
  395.  Given a string, position and length of a word to swap out, and
  396.  a new word to swap in, this function does the swap
  397.  **************************************************************/
  398. void exchange_words(char *string, int word_start, int word_len,
  399.                     char *new_word)
  400. {
  401.     /* This function makes the assumption that all the values given to
  402.      * it are valid.
  403.      */
  404.  
  405.     char temp_storage[MAX_BUFFER];
  406.  
  407. #ifdef DEBUG
  408. char cnum;
  409.  
  410. printf("  exchanging [");
  411. for (cnum = word_start; cnum < word_start+word_len; cnum++)
  412. printf("%c", *(string+cnum));
  413.  
  414. printf("] with [%s]\n", new_word);
  415. #endif
  416.  
  417.     strncpy(temp_storage, string, word_start);
  418.     temp_storage[word_start] = '\0';
  419.     strcat(temp_storage, new_word);
  420.     strcat(temp_storage, string+word_start+word_len);
  421.     strcpy(string, temp_storage);
  422. }
  423.  
  424. /**************************************************************
  425.  function ucase
  426.  Converts a string to all uppercase
  427.  **************************************************************/
  428. void ucase(char *string)
  429. {
  430.     while (*string != '\0')
  431.     {
  432.         *string = toupper(*string);
  433.         string++;
  434.     }
  435. }
  436.  
  437. /**************************************************************
  438.  function reformat_input
  439.  Converts the input to upper case, removes punctuation, and
  440.  strips redundant white space
  441.  **************************************************************/
  442. void reformat_input(void)
  443. {
  444.     int loop;
  445.  
  446.     /* First, convert it to uppercase */
  447.     ucase (input_string);
  448.  
  449.     /* Second, strip punctuation */
  450.     for (loop = 0; loop < strlen(input_string); loop++)
  451.     {
  452.         while (!isalnum(input_string[loop]) && input_string[loop] != '\'' &&
  453.                input_string[loop] != ' ' && input_string[loop] != '\0')
  454.         {
  455.             memmove(input_string+loop, input_string+loop+1,
  456.                     strlen(input_string)-loop);
  457.         }
  458.     }
  459.  
  460.     /* Third, strip redundant white spaces (spaces and tabs) */
  461.     for (loop = 0; loop < strlen(input_string); loop++)
  462.     {
  463.         while (isws(input_string[loop]) && isws(input_string[loop+1]))
  464.         {
  465.             exchange_words(input_string, loop, 2, " ");
  466.         }
  467.     }
  468.  
  469.     /* Fourth, trim the string */
  470.     loop = 0;
  471.  
  472.     while (isws(input_string[loop]))
  473.     {
  474.         loop++;
  475.     }
  476.  
  477.     memmove(input_string, input_string+loop, strlen(input_string)-loop+1);
  478.  
  479.     loop = strlen(input_string) - 1;
  480.  
  481.     while (isws(input_string[loop]) && input_string[loop] != '\0')
  482.     {
  483.         loop--;
  484.     }
  485.  
  486.     input_string[loop+1] = '\0';
  487. }
  488.  
  489.  
  490. /**************************************************************
  491.  function get_input
  492.  This function reads a line from the user
  493.  returns whether or not the line is useable
  494.  **************************************************************/
  495. int get_input(void)
  496. {
  497.     strcpy(input_string_buffer, "  ");
  498.     memset(input_string, '\0', MAX_BUFFER);
  499.     printf("> ");
  500.     fgets(input_string, MAX_BUFFER, stdin);
  501.     reformat_input();
  502. #ifdef DEBUG
  503. printf("[%s]\n", input_string);
  504. #endif
  505.  
  506.     /* See if the user told Eliza to shutup (thereby quitting the program */
  507.     if (strstr(input_string, "SHUT"))
  508.     {
  509.         printf("O.K. IF YOU FEEL THAT WAY I'LL SHUT UP....\n");
  510.         exit (0);
  511.     }
  512.  
  513.     if (strlen(input_string) == 0)
  514.     {
  515.         printf("I BEG YOUR PARDON?\n");
  516.         return 1;
  517.     }
  518.  
  519.     if (strcmp(input_string, last_input_string) == 0)
  520.     {
  521.         printf("PLEASE DON'T REPEAT YOURSELF!\n");
  522.         return 1;  /* Unuseable */
  523.     }
  524.  
  525.     strcpy(last_input_string, input_string);
  526.     strcat(input_string, "  ");  /* Add two spaces to the end of the string */
  527.  
  528.     return 0;  /* Useable */
  529. }
  530.  
  531.  
  532. /**************************************************************
  533.  function find_keyword
  534.  Searches the input string for a known keyword
  535.  **************************************************************/
  536. char *find_keyword(void)
  537. {
  538.     static char found[MAX_BUFFER];
  539.  
  540.     found[0] = '\0';  /* Empty out the string */
  541.  
  542.     for (key = 0; key <= n1; key++)
  543.     {
  544.         if (strstr(input_string_buffer, keyword[key]))
  545.         {
  546.             /* Keyword has been identified */
  547.             strcpy (found, keyword[key]);
  548.             break;
  549.         }
  550.     }
  551.  
  552.     if (key > n1)
  553.     {
  554.         key = n1;  /* In case no key was found */
  555.     }
  556.  
  557.     return found;
  558. }
  559.  
  560. /**************************************************************
  561.  function conjugate
  562.  Takes a part of the input string and conjugates it using the
  563.  list of strings to be swapped.
  564.  **************************************************************/
  565. char *conjugate(char *found)
  566. {
  567.     static char tail[MAX_BUFFER];
  568.     char *tail_ptr;
  569.     char string_pos;
  570.     int word;
  571.  
  572. #ifdef DEBUG
  573. puts("Inside conjugate");
  574. puts("  Extracting end of input string");
  575. #endif
  576.     /* Extract the end of the input string */
  577.     tail_ptr = strstr(input_string_buffer, found) + strlen(found);
  578.     strcpy(tail, " ");
  579.     strcat(tail, tail_ptr);
  580.     strcat(tail, " ");
  581.  
  582. #ifdef DEBUG
  583. printf("  tail = [%s]\n", tail);
  584. printf("  strlen(tail) = %i\n", strlen(tail));
  585. puts("  Swapping first and second person phrases");
  586. #endif
  587.     /* swap first and second person phrases */
  588.     for (string_pos = 0; string_pos < strlen(tail); string_pos++)
  589.     {
  590.         for (word = 0; word <= n2; word++)
  591.         {
  592.             tail_ptr = tail + string_pos;
  593.  
  594.             /* Search through the string for phrases character by character */
  595.             if (strncmp(wordin[word], tail_ptr, strlen(wordin[word])) == 0)
  596.             {
  597.                 exchange_words(tail, string_pos, strlen(wordin[word]), wordout[word]);
  598.                 string_pos = string_pos + strlen(wordout[word]) - 2;
  599.                 break;
  600.             }
  601.         }
  602.     }
  603.  
  604.     return tail;
  605. }
  606.  
  607.  
  608. /**************************************************************
  609.  function response
  610.  Builds and prints a response to the user input
  611.  **************************************************************/
  612. void response(char *tail)
  613. {
  614.     char output[MAX_BUFFER];
  615.  
  616.     strcpy (output, replies[current_response[key]]);
  617.     current_response[key]++;
  618.  
  619.     if (current_response[key] > last_response[key])
  620.     {
  621.         current_response[key] = first_response[key];
  622.     }
  623.  
  624.     if (output[strlen(output) - 1] != '*')
  625.     {
  626.         puts(output);
  627.         return;
  628.     }
  629.  
  630.     if (strcmp(tail, "  ") == 0)
  631.     {
  632.         puts("YOU WILL HAVE TO ELABORATE MORE FOR ME TO HELP YOU\n");
  633.         return;
  634.     }
  635.  
  636.     output[strlen(output)-1] = '\0';
  637.     printf("%s%s\n", output, tail);
  638. }
  639.  
  640. /**************************************************************/
  641.  
  642. main ()
  643. {
  644.     char *found_keyword, *tail;
  645.  
  646.     init();
  647.     printf("HI!  I'M ELIZA.  WHAT'S YOUR PROBLEM?\n");
  648.  
  649.     for (;;)  /* An infinte loop, I'm afraid */
  650.     {
  651. #ifdef DEBUG
  652. puts("Before reading input");
  653. #endif
  654.         if (get_input() != 0)
  655.         {
  656.             continue;  /* Unuseable input - try again */
  657.         }
  658. #ifdef DEBUG
  659. puts("Before searching for keyword");
  660. #endif
  661.         found_keyword = find_keyword();
  662. #ifdef DEBUG
  663. puts("After searching for keyword");
  664. #endif
  665.  
  666.         if (found_keyword[0] != '\0')
  667.         {
  668. #ifdef DEBUG
  669. printf("Found keyword [%s] - conjugating\n", found_keyword);
  670. #endif
  671.             tail = conjugate(found_keyword);
  672. #ifdef DEBUG
  673. puts("Done conjugating");
  674. #endif
  675.         }
  676.         else
  677.         {
  678.             tail = "";
  679.         }
  680.  
  681. #ifdef DEBUG
  682. puts("Building response");
  683. #endif
  684.         response(tail);
  685. #ifdef DEBUG
  686. puts("Done building response");
  687. #endif
  688.     }
  689. }
  690.